/**
 * Created by duwei on 2019/3/28.
 */
var ZiPai_JieSuanJavaWindow = ZiPai_JieSuanWindow.extend({
    /**
     * 胡牌类型
     */
    HU_TYPE: {
        LIUJU: 0,
        ZIMO: 1,
        DIANPAO: 2,
        JIESAN: 3
    },
    PAI_TYPE:['','','吃','坎','碰','偎','臭偎','提','跑'],
    /**
     * 初始化界面
     * @private
     */
    _initInfo: function () {
        var self = this;
        self.isZimo = (self._data['hu_type'] == self.HU_TYPE.ZIMO);
        self.isLiuju = (self._data['hu_type'] == self.HU_TYPE.LIUJU);
        self.isDianpao = (self._data['hu_type'] == self.HU_TYPE.DIANPAO);
        self.isJiesan = (self._data['hu_type'] == self.HU_TYPE.JIESAN);
        self._setTitle();
        self._setWanFa();
        self._setTime();
        self._setRoomId();
        self._setPlayers();
        self._setDipai();
        self._setBtn();
    },
    /**
     * 设置标题
     * @private
     */
    _setTitle: function () {
        var self = this;
        var shengli = this._nodeList['root.shengli'];
        var shibai = this._nodeList['root.shibai'];
        var huanghzuang = this._nodeList['root.huanghzuang'];
        huanghzuang.setVisible(self.isLiuju);
        shengli.setVisible(self.Winner == gameData.uid);
        shibai.setVisible(self.isJiesan || self.Winner != gameData.uid);
    },
    /**
     * 设置时间
     * @private
     */
    _setTime: function () {
        var self = this;
        self._nodeList["root.lb_ts"].setString(timestamp2time(self._data['ts']));
    },
    /**
     * 设置玩家数据
     * @private
     */
    _setPlayers: function () {
        var self = this;
        this.myturn = 0;
        this.winnerturn = 0;
        self._nodeList["root.player2"].setVisible(gameData.playerNum == 2);
        self._nodeList["root.player3"].setVisible(gameData.playerNum == 3);
        self.playerNode = self._nodeList["root.player" + gameData.playerNum];

        var players = deepCopy(self._data['players']);
        for (var i = 0; i < players.length; i++) {
            var player = players[0];
            if (!player['hu']) {
                players.splice(0, 1);
                players.push(player);
            } else{
                self.Winner = player['uid'];
                break;
            }
        }

        for (var i = 0; i < players.length; i++) {
            var player = players[i];
            var uid = player['uid'];
            var score = player['Result'];
            if (uid == gameData.uid) {
                self.my_score = score;
                self.myturn = i + 1;
            }
            if (uid == self._data['Winner']) {
                this.winnerturn = i + 1;
            }
            self._setRowUnit(i, player);
        }
    },
    /**
     * 设置玩家牌数据
     * @param index
     * @param player
     * @private
     */
    _setRowUnit: function (index, player) {
        var self = this;
        var uid = player.uid;
        var playerInfo = gameData.getPlayerInfoByUid(uid);
        var RowUnit = self.playerNode["Player" + index];
        RowUnit['playerNode']["lb_name"].setString(ellipsisStr(playerInfo['nickname'], 6));
        RowUnit['playerNode']["lb_id"].setString('ID:' + uid);
        loadHeadImage(playerInfo['headimgurl'], RowUnit['playerNode']['head'], true);//头像
        RowUnit['bg_ziji'] && RowUnit['bg_ziji'].setVisible(self.Winner && (index == 0));
        var zhuangUid = self._data['zhuang'];
        RowUnit['playerNode']["zhuang"].setVisible(zhuangUid == uid);
        self._setRowCard(index, player);
        self.setMingTang(index);
    },
    /**
     * 设置玩家显示位置
     * @private
     */
    _setRowCard: function (index, player) {
        var self = this;
        var RowUnit = self.playerNode["Player" + index];
        var DiscardCards = player['usedPai'];
        var Hands = player['shoupai'];
        var Menzi = player['duiArr'];
        var nCombos = RowUnit['nCombos'];
        var ComBoInfo0 = nCombos['ComBoInfo0'];
        var count = 0;
        var huxi = 0;
        var isWinner = false ;
        if(Menzi){
            Menzi.sort(function (a, b) {
                return b['pai_arr'].length - a['pai_arr'].length;
            });
            self.winCards = [];
            for (var i = 0; i < Menzi.length; i++) {
                var mz = Menzi[i];
                var comboInfo = {};
                comboInfo['Cards'] = mz['pai_arr'];
                comboInfo['Type'] = self.PAI_TYPE[mz['type']];
                comboInfo['Xi'] =  gameData.mapId == app.map_id.XIANGXI_ERQISHI?'':mz['huXi'];
                huxi += comboInfo['Xi'];
                self.setComBoInfo(index, count, comboInfo,(player['hu'] && mz['isHuDui'])?self._data['hu_pai_id']:null);
                count++;
            }
            if(player['hu'] && self.winCards.length > 0){
                isWinner = true ;
                for (var s = self.winCards.length - 1; s >= 0; s--) {
                    var card = self.winCards[s];
                    if (self._data['hu_pai_id'] && self._data['hu_pai_id'] == card.data) {
                        var ComBoInfoNode = card.getParent().getParent();
                        var hu_icon = ComBoInfoNode.getChildByName('pai_hu');
                        hu_icon.setVisible(true);
                        hu_icon.setPositionY(hu_icon.posY +  card.index * self._cardSize);
                        break;
                    }
                }
            }
        }
        if(Hands && Hands.length && !player['hu']){
            var CardList = mCard.getCardList(Hands);
            var shoupaiHuxi =  mCard.getHandsHuXi(CardList) ;
            huxi += shoupaiHuxi;
            self.setShouPaiInfo(index, count, Hands,shoupaiHuxi);
        }
        //弃牌
        self.setQiPaiInfo(index, DiscardCards);
        player['HuXi'] = huxi;
        self.setFenShu(index, player,isWinner);
    },
    /**
     * 设置分数
     * @private
     */
    setFenShu: function (index, player, isWinner) {
        var self = this;
        var RowUnit = self.playerNode["Player" + index];
        var tunshu = RowUnit['tunshu'];//囤数
        var fanshu = RowUnit['fanshu'];//番数
        var huxi = RowUnit['huxi'];//胡息
        var fenshu = RowUnit['fenshu'];//分数

        var lb_tunshu = RowUnit['lb_tunshu'];//囤数
        var lb_fanshu = RowUnit['lb_fanshu'];//番数
        var lb_huxi = RowUnit['lb_huxi'];//胡息
        var lb_fenshu = RowUnit['lb_fenshu'];//分数

        if(gameData.mapId == app.map_id.XIANGXI_ERQISHI){
            fenshu.setPosition(tunshu.getPosition());
            lb_fenshu.setPosition((lb_tunshu.getPosition()));
            lb_tunshu.setVisible(false);//囤数
            lb_fanshu.setVisible(false);//番数
            lb_huxi.setVisible(false);//胡息
            tunshu.setVisible(false);//囤数
            fanshu.setVisible(false);//番数
            huxi.setVisible(false);//胡息
            self.setFenShuColor(lb_fenshu, player['score']);
            return;
        }else  if(gameData.mapId == app.map_id.NXPHZ){
            tunshu.setString('底数:');
            fanshu.setString('名堂:');
        }

        tunshu.setVisible(isWinner);
        fanshu.setVisible(isWinner && player['win_fan']);
        lb_tunshu.setVisible(isWinner);
        lb_fanshu.setVisible(isWinner && player['win_fan']);

        if (isWinner) {
            self.setFenShuColor(lb_tunshu, player['win_tun']||0);
            self.setFenShuColor(lb_fanshu, player['win_fan']||0);
            self.setFenShuColor(lb_huxi, player['HuXi']);
            self.setFenShuColor(lb_fenshu, player['score']);
        } else {
            self.setFenShuColor(lb_huxi, player['HuXi']);
            self.setFenShuColor(lb_fenshu, player['score']);
        }
    },
    /**
     * 设置名堂
     * @private
     */
    setMingTang: function (index) {
        var self = this;
        var FanRemark = self._data['fanInfo'];
        //FanRemark = ['碰碰胡','海底胡','红胡','点胡'];
        var RowUnit = self.playerNode["Player" + index];
        var mingTangNode = RowUnit['mingTangNode'];
        if (!mingTangNode) {
            return;
        }
        var isMingTang = (index == 0 && FanRemark.length > 0);
        mingTangNode.setVisible(isMingTang);
        var mingtang0 = mingTangNode['mingtang0'];
        mingtang0.setVisible(false);
        if (isMingTang) {
            for (var i = 0; i < FanRemark.length; i++) {
                var obj = FanRemark[i];
                var mingtangStr = '';
                for (var key in obj) {
                    mingtangStr = decodeURIComponent(key)+ 'x' + obj[key];
                }
                if (mingtangStr == '') {
                    break;
                }
                var mingtang = mingTangNode.getChildByName('mingtang' + i);
                if (!mingtang) {
                    mingtang = duplicateNode(mingtang0);
                    mingtang.setPosition(cc.p(0 -parseInt(i/5)*160, -(i%5) * 65));
                    mingTangNode.addChild(mingtang);
                }
                mingtang.setVisible(true);
                mingtang.setString(mingtangStr);
                var size = mingtang.getContentSize();
                mingtang['kuang'].setContentSize(cc.size(size.width + 40, size.height + 15));
                mingtang['kuang'].setPosition(cc.p(size.width / 2, size.height / 2));
            }
        }
    },
    /**
     * 设置底牌
     * @private
     */
    _setDipai: function () {
        var count = 12;
        var self = this;
        var LeftCards = self._data['dipai_arr'] || [];
        var Sipai = self._data['sipai_arr'] || [];
        var dipaiNode = self._nodeList['root.dipaiNode'];
        dipaiNode['bg2'].setVisible(gameData.playerNum == 2);
        dipaiNode['bg3'].setVisible(gameData.playerNum == 3);
        dipaiNode['choupaiNode'].setVisible(gameData.playerNum == 2 && Sipai && Sipai.length > 0);

        for (var i = 0; i < LeftCards.length; i++) {
            var x = i % count;
            var y = parseInt(i / count);
            var card = new Card();
            card.setData(LeftCards[i], null, 2);
            card.setScale(self._cardSize / 42);
            card.setAnchorPoint(0, 0);
            card.setPosition(x * self._cardSize, y * -35);
            dipaiNode['dipaiPos0'].addChild(card);

            if(i == LeftCards.length-1){
                var di_icon = duplicateNode(dipaiNode['pai_di']);
                di_icon.setVisible(true);
                di_icon.setPosition(x * self._cardSize, y * -35);
                dipaiNode['dipaiPos0'].addChild(di_icon);
            }
        }
        if (Sipai && Sipai.length > 0) {
            for (var i = 0; i < Sipai.length; i++) {
                var x = i % count;
                var y = parseInt(i / count);
                var card = new Card();
                card.setData(Sipai[i], null, 2);
                card.setScale(self._cardSize / 42);
                card.setAnchorPoint(0, 0);
                card.setPosition(x * self._cardSize, y * -35);
                dipaiNode['choupaiNode']['choupaiPos0'].addChild(card);
            }
        }
    },
})